<%@ page language="java" import="java.util.*,ll.sms.pojo.*" pageEncoding="UTF-8"%>
<%@page import="ll.sms.util.*"%>
<link rel="stylesheet" type="text/css" href="../ext/ext-all.css" />
<script type="text/javascript" src="../ext/ext-base.js"></script>
<script type="text/javascript" src="../ext/ext-all.js"></script>
<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="../js/copy.js"></script>
<%
List<ChannelCombox> list = SmsUtil.getChannelList();
%>
<style type="text/css">

.ext-ie .x-menu-item-icon {
	left: -24px;
	top: 0px;
}

.ext-strict .x-menu-item-icon {
	left: 3px;
	top: 3px;
}

.ext-ie6 .x-menu-item-icon {
	left: -24px;
	top: 0px;
}
</style>

<script type="text/javascript">
Ext.BLANK_IMAGE_URL = '../images/s.gif';
 window.onload =(function(){

	var store1 = new Ext.data.SimpleStore({
	fields: ['clazz', 'showname'],
	data : <%=ExtJsonBuilder.channel2ArrayString(list)%>
	});

    var comb = new Ext.form.ComboBox({
            emptyText : '请选择',
            store: store1,
            mode:'local',
            triggerAction:'all',
            valueField: 'clazz',
            name:'channel_',
            id:'channel_',
            hiddenName : 'channel_1',
            displayField:'showname',
 			fieldLabel: '通道',
 			withd :120
            });

      var ds = new Ext.data.Store({//这是数据源
           proxy : new Ext.data.HttpProxy({url:'../ajaxjsp/sendedmessage.jsp'}),
           reader: new Ext.data.JsonReader({
               root: 'result',
               totalProperty: 'totalCount'

              },[
              'mid','mfrom','mto','mcontent','msendtime','maddtime','msendtype','mstatue','mttype','mchannel'
          ])
     });

      var sm = new Ext.grid.CheckboxSelectionModel();
      sm.handleMouseDown = Ext.emptyFn;
      
    var colModel = new Ext.grid.ColumnModel([//定义列
          new Ext.grid.RowNumberer(),
          sm,
          {header:'发送者',width:100,sortable:true,dataIndex:'mfrom'},
          {header:'接收者',width:100,sortable:true,dataIndex:'mto'},
          {header:'内容',width:200,sortable:true,dataIndex:'mcontent'},
          {header:'发送时间',width:100,sortable:true,dataIndex:'msendtime'},
          {header:'添加时间',width:100,sortable:true,dataIndex:'maddtime'},
          {header:'发送类型',width:100,sortable:true,dataIndex:'msendtype',renderer:function(val){return val==1?'单发':'群发';}},
          {header:'发送状态',width:100,sortable:true,dataIndex:'mstatue',renderer:function(val){
          	
          	if(val==-2 ){
          	return '失败';
          	}else if(val==-3){
          	return '取消';
            }else{
          	return '成功';
          	}

          }}
         // {header:'消息类型',width:100,sortable:true,dataIndex:'mttype'},
          //{header:'所走通道',width:100,sortable:true,dataIndex:'mchannel'}
      ]);

     var grid = new Ext.grid.GridPanel({//列表
                 border:false,
                 region:'center',
                 loadMask: true,
                 renderto:'center',
                 title:'已发送列表',
                 store: ds,
                 cm: colModel,
                 sm: sm,
                 autoScroll: true,
                 bbar: new Ext.PagingToolbar({
                 pageSize: 20,
                 store: ds,
                 displayInfo: true,
                 displayMsg: '第{0} 到 {1} 条数据 共{2}条',
                 emptyMsg: "没有数据"
                 }),
                 tbar: new Ext.Toolbar({
                     items:[
                            {
                                  id:'del'
                                 ,text:"删除"
                                 ,iconCls:'settings'
                                 ,handler: function(){
                                 doGrid();
                                 }
                             }

                         ]
                     })
              });


     grid.on("cellclick",function(grid, rowIndex, columnIndex, e)
    	        {
	    	 e.preventDefault();if(rowIndex<0){return;}
	         var record = grid.getStore().getAt(rowIndex);   //Get the Record
			 var fieldName = grid.getColumnModel().getDataIndex(columnIndex); //Get field name
			 var data = record.get(fieldName);
	    	 if(fieldName!='mto' && fieldName!='mcontent'){
					return;
	        	 }
	         var treeMenu = new Ext.menu.Menu
    	            ([
    	                {xtype:"button",text:"复制到剪贴板",icon:"../img/copy.gif",pressed:true,handler:function(){
    		                if(Ext.isIE){
        		                if(fieldName=='mto'){
    			                copy_clip(data.replace(/,/g,'\n'));
        		                }else{
        		                copy_clip(data);
            		            }
    			                alert('['+data+']已经复制到剪贴板');
    		                }else{
    		                	alert('对不起,剪贴板只支持IE浏览器复制!!');
    		                }
    	                }}
    	            ]);
    	            treeMenu.showAt(e.getPoint());
    	        });
     
       var top = new Ext.FormPanel({//这里是搜索表单
          //buttonAlign:'right',
          labelWidth:50,
          region:'east',
          width:'30%',
          url:'../ajaxjsp/sendedmessage.jsp',
          frame:true,
          title: '搜索',
          items: [{
                   xtype:'textfield',
                   fieldLabel: '发送者',
                   id: 'mfrom',
                   name: 'mfrom'
                  },
                  {
                   xtype:'textfield',
                   fieldLabel: '接受者',
                   id: 'mto',
                   name: 'mto'
                  },
                new Ext.form.ComboBox({
                fieldLabel: '发送状态',
                name:'sendstatu',
                id : 'sendstatu',
                hiddenName :'sendstatu_',
                store: new Ext.data.SimpleStore({
                    fields: ['abbr', 'state'],
                    data : [
                    		[0,'所有'],
        					[1,'成功'],
        					[-2,'失败'],
        					[-1,'取消']
        			   		]
                }),
                valueField:'abbr',
                displayField:'state',
                typeAhead: true,
                mode: 'local',
                triggerAction: 'all',
                emptyText:'请选择...',
                selectOnFocus:true,
               // value:'是',
                readOnly:true
                //width:'50%'
            }),
			comb
            ,{
                   xtype:'textfield',
                   fieldLabel: '发送时间',
                   id: 'msendtime',
                   name: 'msendtime',
                   listeners:{
            	   focus:function(cmp) {
            	  // alert(new WdatePicker());
               	  //new WdatePicker(cmp.getEl().dom, '%Y-%M-%D %h:%m:%s', true)
           		      new WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});
           		      }
        		   }
                  },{
                   xtype:'textfield',
                   fieldLabel: '至',
                   id: 'msendtime_1',
                   name: 'msendtime_1',
                   listeners:{
            	   focus:function(cmp) {
            	   // alert(new WdatePicker());
               	   //new WdatePicker(cmp.getEl().dom, '%Y-%M-%D %h:%m:%s', true)
           		      new WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});
           		      }
                  }
                  }
          ],

           buttons: [{
               text: '查询',
               handler:function(){
                         // 这里是关键，重新载入数据源，并把搜索表单值提交
                      ds.load(
                      {params:
                      {
                      start:0,
                      limit:20,
                      username:Ext.get('mfrom').dom.value,
                      tousername :Ext.get('mto').dom.value,
                      msendtime : Ext.get('msendtime').dom.value,
                      msendtime_1 : Ext.get('msendtime_1').dom.value,
                      channel_ : Ext.get('channel_1').getValue(),
                      sendstatu : Ext.get('sendstatu_').getValue()
                     }
                     });
                }
              },{
              text:'重置',
			  handler:function(){
			  	top.form.reset();
			  }
              },
              {
                  text:'导出Excel',
    			  handler:function(){
				  var url = "downloadexcel.jsp?username="+Ext.get('mfrom').dom.value+"&tousername="+Ext.get('mto').dom.value+"&msendtime="+Ext.get('msendtime').dom.value+"&msendtime_1="+Ext.get('msendtime_1').dom.value+"&channel_="+Ext.get('channel_1').getValue()+"&sendstatu="+Ext.get('sendstatu_').getValue();
            	  window.open (url, 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
    			  }
             }
          ]
      });


      var viewport = new Ext.Viewport({
         layout:'border',
          items:[
              top,
              grid
          ]}
      );




      ds.load({params:{start:0, limit:20}});
          // 这里很关键，如果不加，翻页后搜索条件就变没了，这里的意思是每次数据载入前先把搜索表单值加上去，这样就做到了翻页保留搜索条件了
      ds.on('beforeload',function(){
          Ext.apply(
          this.baseParams,
          {
              username:Ext.get('mfrom').dom.value,
			  tousername :Ext.get('mto').dom.value,
              msendtime : Ext.get('msendtime').dom.value,
              msendtime_1:Ext.get('msendtime_1').dom.value,
              channel_ : Ext.get('channel_1').getValue(),
              sendstatu : Ext.get('sendstatu_').getValue()
          });
     });


      function doGrid(){
              var rows = grid.getSelectionModel().getSelections();
              if (rows.length == 0) {
                  Ext.MessageBox.alert('错误', '最少选择一条信息，进行操作!');
              }
              else {
                  Ext.MessageBox.confirm('警告', '你确定删除所选消息吗？', function(btn){
                      if (btn == 'yes') {
                      var delid='';
                          for (var i = 0; i < rows.length; i++) {
                              delid=delid+rows[i].get('mid')+'^';
                          }
                        Ext.Ajax.request({
                        url : '../ajaxjsp/system.jsp?flag=delmessages',
                        method : 'POST',
                        params : {
                            delid : delid
                        },
                        success: function(response, options) {
                              //获取响应的json字符串
                             var responseArray = Ext.util.JSON.decode(response.responseText);
                                  if(responseArray.success==true){
                                  	 Ext.Msg.alert('成功','操作成功');
      								grid.getStore().reload();
                                  }
                                  else{
                                      Ext.Msg.alert('失败','删除操作失败');
                                  }
                               }
                    });
                      }
                  });
              }
          }
});
 </script>

<div id="north-div"></div>
<div id="center"></div>
